package com.mogujie.tt.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.mogujie.tt.R;
import com.mogujie.tt.ui.helper.Emoparser;
import com.mogujie.tt.ui.widget.QuanzDynamicImageGridView;
import com.mogujie.tt.ui.widget.QuanzMypotoGridDetail;
import com.mogujie.tt.ui.widget.QuanzQzMyPhotoDetail;
import com.mogujie.tt.ui.widget.QuanzQzPhone;
import com.mogujie.tt.utils.IMUIHelper;

import net.tsz.afinal.FinalBitmap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/11/13.
 */
public class MypotoGridViewitemAdapter extends BaseAdapter implements View.OnClickListener{
    public Context context;
    public List<QuanzMypotoGridDetail> flist = null;
    private QuanzMypotoGridDetail taillist = null;
    private QuanzQzMyPhotoDetail hotodetail = null;
    private FinalBitmap finalBit;

    public MypotoGridViewitemAdapter(Context context,List<QuanzMypotoGridDetail> flist){
        this.context = context;
        this.flist = flist;
        finalBit = FinalBitmap.create(context);
    }
    @Override
    public boolean areAllItemsEnabled() {
        return false;
    }

    @Override
    public boolean isEnabled(int position) {
        return false;
    }
    @Override
    public int getCount() {
        return flist == null ? 0 : flist.size();
    }

    @Override
    public Object getItem(int i) {
        return flist == null ? null : flist.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        try {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.quanz_qz_myphoto_item_f, null);
            holder = getHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (flist != null) {
            taillist=flist.get(position);
            taillist.Gposition=position;
            bindData(holder);
        }
        return convertView;
        }catch (Exception e){
            System.out.println("1111111MypotoGridViewitemAdapter   " + e.getMessage());
        }
        return null;
    }

    private void bindData(ViewHolder holder) {

        hotodetail=new QuanzQzMyPhotoDetail();

        if (taillist.GP.isEmpty()){
            holder.LL_image.setVisibility(View.GONE);
        }else{
            holder.LL_image.setVisibility(View.VISIBLE);
            holder.LL_image.setOnClickListener(this);
            holder.LL_image.setTag(taillist);
        }

        if (taillist.getGcontentText().isEmpty()) {
            holder.LL.setVisibility(View.GONE);
        } else {
            holder.LL.setVisibility(View.VISIBLE);
        }
        holder.ftv.setText(Emoparser.getInstance(context).emoCharsequence(taillist.getGcontentText()));

        if (taillist.getGphoto().isEmpty()){
            holder.image_content.setVisibility(View.GONE);
        }else{
            holder.image_content.setVisibility(View.VISIBLE);
            final ArrayList<QuanzQzPhone> list = getPhotos(taillist.getGphoto());
            if (list != null) {
                holder.image_content.setAdapter(new QuanzQzImageAdapter(context, list, false));
            }
        }

        if (taillist.getGimages().isEmpty()){
            holder.LL_i_h.setVisibility(View.GONE);
        }else{
            holder.LL_i_h.setVisibility(View.VISIBLE);
            finalBit.display(holder.imageView, taillist.getGimages());
        }


        holder.LL.setOnClickListener(this);
        holder.LL.setTag(taillist);

    }

    private ArrayList<QuanzQzPhone> getPhotos(String photo) {
        if (!photo.contains("[")) {
            return null;
        }
        ArrayList<QuanzQzPhone> phs = new ArrayList<QuanzQzPhone>();
        try {
            JSONArray array = new JSONArray(photo);
            int size = array.length();
            JSONObject obj;
            QuanzQzPhone ph;
            for (int i = 0; i < size; i++) {
                obj = array.getJSONObject(i);
                ph = new QuanzQzPhone();
                ph.min = obj.getString("attach_small");
                ph.max = obj.getString("attach_url");
                phs.add(ph);
            }
            return phs;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
    private ViewHolder getHolder(View convertView) {

        ViewHolder holder = new ViewHolder();
        holder.ftv = (TextView) convertView.findViewById(R.id.two_tv);
        holder.LL = convertView.findViewById(R.id.LL_text);
        //holder.LL_im = (ImageView) convertView.findViewById(R.id.imageButton);
        holder.LL_image = convertView.findViewById(R.id.LL_image);
        holder.imageView = convertView.findViewById(R.id.imagehead);
        holder.image_content = (QuanzDynamicImageGridView) convertView.findViewById(R.id.image_content);
        holder.LL_i_h = convertView.findViewById(R.id.LL_i_h);
        return holder;
    }

    public static int feed_Id;
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.LL_text:
                taillist = (QuanzMypotoGridDetail) view.getTag();
                feed_Id = taillist.friendId;
                IMUIHelper.openQzDetailsActivity(context, taillist.friendId);
//                System.out.println("7777"+taillist.type);
//                if (taillist.type.equals("postimage")){
//                    final ArrayList<QuanzQzPhone> list = getPhotos(taillist.getGphoto());
//                    ArrayList<String> urls1=new ArrayList<String>();
//                    for (int i=0;i<list.size();i++){
//                        QuanzQzPhone urls2 = (QuanzQzPhone)list.get(i);
//                        urls1.add(urls2.max);
//                    }
//                    Intent intent = new Intent(context, ImagePagerActivity.class);
//                    intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, urls1);
//                    intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, 0);
//                    context.startActivity(intent);
//                }else {
//                    feed_Id = taillist.friendId;
//                    IMUIHelper.openQzDetailsActivity(context, taillist.friendId);
//                }
                break;
            case R.id.LL_image:
                taillist= (QuanzMypotoGridDetail) view.getTag();
                System.out.println("77888888888");
                IMUIHelper.openQuanzQzPublishActivity(context, 1);
                break;
        }
    }
    class ViewHolder {
        public QuanzDynamicImageGridView gridView;
        public TextView ftv;
        public View LL;
        public ImageView LL_im;//griview
        public View LL_image;
        public View LL_i_h;
        public View imageView;//文字图片
        public QuanzDynamicImageGridView image_content;
    }
}
